package tools;

import java.awt.Color;
import java.awt.Font;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Preferences implements Serializable {
	
	private static final long serialVersionUID = 1L;
	public static final String preferencesFile = "tabinatorPrefs.prf";
	
	// display settings
	int[] searchColumnWidths = {94,314,34,15};
	private int localTabSplitPosition = 175;
	private Font font;
	private Color textForegroundColor = Color.BLACK;
	private Color textBackgroundColor = Color.WHITE;

	// location settings
	String tabRootDirectory = "";
	
	
	public Preferences()	{
		
	}

	public void setSearchColumnWidths(int col, int width)	{
		searchColumnWidths[col] = width;
	}

	public void setSearchColumnWidths(int[] scw)	{
		searchColumnWidths = scw;
	}

	public void setTabRootDirectory(String path)	{
		tabRootDirectory = path;
	}

	public String getTabRootDirectory()	{
		return tabRootDirectory;
	}
	
	public int getSearchColumnWidths(int col)	{
		return searchColumnWidths[col];
	}
	
	public int[] getSearchColumnWidths()	{
		return searchColumnWidths;
	}
	
	public boolean save()	{
		FileOutputStream fos = null;
		ObjectOutputStream out = null;
		try
		{
			fos = new FileOutputStream(Preferences.preferencesFile);
			out = new ObjectOutputStream(fos);
			out.writeObject(this);
			out.close();
			return true;
		}
		catch(IOException ex)	{
			ex.printStackTrace();
			return false;
		}
	}	

	public static Preferences readPrefsFile(String file)	{
		FileInputStream fos = null;
		ObjectInputStream in = null;
		Preferences prefs;

		try	{
			fos = new FileInputStream(file);
			in = new ObjectInputStream(fos);
			prefs = (Preferences) in.readObject();
			in.close();
			return prefs;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (FileNotFoundException e) {
			System.err.println("Error Reading " + file + " File Not Found!");
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			System.err.println("Invalid tab file!");
			e.printStackTrace();
			return null;
		}
	}

	public void setLocalTabSplitPosition(int pos) {
		localTabSplitPosition = pos;
	}	

	public int getLocalTabSplitPosition()	{
		return localTabSplitPosition;
	}
	
	public Font getPreferredFont()	{
		return font;
	}
	
	public void setPreferredFont(Font f)	{
		font = f;
	}

	public Color getTextForegroundColor() {
		if( textForegroundColor == null )
			System.out.println("NULL FG COLOR");
		return textForegroundColor;
	}

	public void setTextForegroundColor(Color textForegroundColor) {
		this.textForegroundColor = textForegroundColor;
	}

	public Color getTextBackgroundColor() {
		if( textBackgroundColor == null )
			System.out.println("NULL BG COLOR");
		
		return textBackgroundColor;
	}

	public void setTextBackgroundColor(Color textBackgroundColor) {
		this.textBackgroundColor = textBackgroundColor;
	}
	
}
